<template>
  <view class="debug-container">
    <view class="debug-header">
      <text class="debug-title">SN扫描调试页面</text>
    </view>
    
    <view class="debug-section">
      <view class="input-group">
        <text class="input-label">输入SN或条码进行测试</text>
        <input 
          class="debug-input" 
          v-model="testInput"
          placeholder="请输入SN或条码"
        />
        <button class="test-btn" @click="testScan">测试扫描</button>
      </view>
    </view>
    
    <view class="debug-section">
      <text class="section-title">调试信息</text>
      <view class="debug-info">
        <text class="debug-text">输入内容: {{ testInput }}</text>
        <text class="debug-text">判断为SN: {{ isSnResult }}</text>
        <text class="debug-text">使用接口: {{ apiUsed }}</text>
      </view>
    </view>
    
    <view class="debug-section">
      <text class="section-title">API响应</text>
      <view class="response-info">
        <text class="response-text">{{ apiResponse }}</text>
      </view>
    </view>
    
    <view class="debug-section">
      <text class="section-title">测试用例</text>
      <view class="test-cases">
        <button 
          v-for="(testCase, index) in testCases" 
          :key="index"
          class="case-btn"
          @click="runTestCase(testCase)"
        >
          {{ testCase.name }}
        </button>
      </view>
    </view>
  </view>
</template>

<script>
import { 
  getInStockBySnCode,
  getInStockDetailBySnCode,
  scanInStock,
  getMaterialByBarcode,
  getMaterialBySn
} from '@/api/productrecpt'

export default {
  name: 'DebugPage',
  data() {
    return {
      testInput: '',
      isSnResult: false,
      apiUsed: '',
      apiResponse: '暂无响应',
      testCases: [
        { name: '测试SN格式1', value: 'SN123456789' },
        { name: '测试SN格式2', value: 'AB1234567890' },
        { name: '测试条码格式', value: 'P.05.00681*032506807478' },
        { name: '测试纯数字SN', value: '123456789012345' }
      ]
    }
  },
  
  methods: {
    // 测试扫描
    async testScan() {
      if (!this.testInput.trim()) {
        uni.showToast({
          title: '请输入测试内容',
          icon: 'none'
        })
        return
      }
      
      this.isSnResult = this.isSerialNumber(this.testInput)
      this.apiUsed = this.isSnResult ? 'getInStockBySnCode' : 'getMaterialByBarcode'
      
      try {
        uni.showLoading({
          title: '测试中...'
        })
        
        let response
        if (this.isSnResult) {
          // 测试新的SN接口
          response = await getInStockBySnCode(this.testInput)
        } else {
          // 测试条码接口
          response = await getMaterialByBarcode(this.testInput)
        }
        
        this.apiResponse = JSON.stringify(response, null, 2)
        
        uni.showToast({
          title: '测试完成',
          icon: 'success'
        })
      } catch (error) {
        this.apiResponse = `错误: ${error.message || error}`
        uni.showToast({
          title: '测试失败',
          icon: 'none'
        })
      } finally {
        uni.hideLoading()
      }
    },
    
    // 运行测试用例
    runTestCase(testCase) {
      this.testInput = testCase.value
      this.testScan()
    },
    
    // 判断是否为序列号
    isSerialNumber(input) {
      const snPatterns = [
        /^SN\d+/,  // 以SN开头的数字
        /^[A-Z]{2}\d{8,}/,  // 2个字母+8位以上数字
        /^\d{10,}/  // 10位以上纯数字
      ]
      
      return snPatterns.some(pattern => pattern.test(input))
    }
  }
}
</script>

<style lang="scss" scoped>
.debug-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.debug-header {
  text-align: center;
  margin-bottom: 30rpx;
  
  .debug-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
  }
}

.debug-section {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  
  .section-title {
    font-size: 28rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }
}

.input-group {
  .input-label {
    display: block;
    font-size: 26rpx;
    color: #666;
    margin-bottom: 15rpx;
  }
  
  .debug-input {
    width: 100%;
    height: 80rpx;
    padding: 0 20rpx;
    border: 1rpx solid #ddd;
    border-radius: 8rpx;
    font-size: 28rpx;
    margin-bottom: 20rpx;
  }
  
  .test-btn {
    width: 100%;
    height: 80rpx;
    background: #007AFF;
    color: #fff;
    border: none;
    border-radius: 8rpx;
    font-size: 28rpx;
  }
}

.debug-info {
  .debug-text {
    display: block;
    font-size: 24rpx;
    color: #333;
    margin-bottom: 10rpx;
    padding: 10rpx;
    background: #f8f9fa;
    border-radius: 4rpx;
  }
}

.response-info {
  .response-text {
    font-size: 22rpx;
    color: #666;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-all;
  }
}

.test-cases {
  display: flex;
  flex-wrap: wrap;
  gap: 15rpx;
  
  .case-btn {
    padding: 15rpx 25rpx;
    background: #f8f9fa;
    color: #333;
    border: 1rpx solid #ddd;
    border-radius: 8rpx;
    font-size: 24rpx;
  }
}
</style>
